//
//  CYImageAdd.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/2/1.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "CYImageAdd.h"
#import "CYImageSubView.h"
@implementation CYImageAdd
{
    QMUIGridView *contentView;
    NSMutableArray *allsubview;//用于移除所有视图
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    _imagearr = [NSMutableArray array];
    NSLog(ASLocalizedString(@"cell 高度%@"),NSStringFromCGRect(frame));
    if(self)
    {
        contentView = [[QMUIGridView alloc] initWithColumn:3 rowHeight:CY_IMAGE_HEIGHT];
        contentView.frame = CGRectMake(10, 15, SCREEN_WIDTH - 20, CY_IMAGE_BG_HEIGHT);
        contentView.separatorWidth = 5;
        contentView.separatorColor = [UIColor clearColor];
        
        [self addSubview:contentView];
    }
    return self;
}

-(void)reloadData
{
//    [contentView removeAllSubviews];//移除所有界面重写
    
    for (CYImageSubView *view in allsubview) {
        [UIView animateWithDuration:0.5 animations:^{
            [view removeFromSuperview];
        }];
    }
    
    allsubview = [NSMutableArray array];
    for (int i=0; i<_imagearr.count; i++) {
        CYImageSubView *subimage;
        //判断是图片还是网络url
        if([[_imagearr[i] class] isEqual:[UIImage class]])
        {
            subimage = [[CYImageSubView alloc] init];
            [subimage initWidthImage:_imagearr[i]];
            subimage.type = @"2";
            
        }
        else
        {
            subimage = [[CYImageSubView alloc] init];
            [subimage initWidthUrl:_imagearr[i]];
            subimage.type = @"1";
        }
        
        subimage.index = i;
        subimage.deleClickBlock = ^(NSString *type, NSString *imgid, int index) {
            //通知主线程刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                //回调或者说是通知主线程刷新，
                if(self.deleClickBlock)
                {
                    self.deleClickBlock(type, imgid, index);
                }
                [_imagearr removeObjectAtIndex:index];
                [self reloadData];
            });
        };
        [contentView addSubview:subimage];
        [allsubview addObject:subimage];
    }
    
    
    if(_imagearr.count < CY_MAX_IMAGE_NUMBER)
    {
        //如果没有超出最大数量就新添加一个➕
        CYImageSubView *subimage = [[CYImageSubView alloc] init];
        [subimage initWidthImage:IMAGE_NAMED(@"mine_private_add")];
        subimage.type = @"3";
        //添加点击事件
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addImage)];
        [subimage addGestureRecognizer:tapGesturRecognizer];
        subimage.userInteractionEnabled = YES;
        
        [contentView addSubview:subimage];
        [allsubview addObject:subimage];
    }
}
- (void)addImage {
    if(self.addClickBlock)
    {
        self.addClickBlock();
    }
}

-(void)addImage:(NSArray *)image
{
    [_imagearr addObjectsFromArray:image];
    [self reloadData];
}
-(void)removieAuditingIndex:(NSInteger)index
{
    CYImageSubView *subimage = allsubview[index];
    subimage.auditLable.hidden = YES;
    
}
-(void)removeAllImage
{
    [_imagearr removeAllObjects];
    [self reloadData];
}
-(void)layoutSubview{
    
    [super layoutSubviews];
    
}
@end
